FrameLib  2.0
DSP processing with frames of arbitrary timing and length
FrameLib_Global.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_GLOBAL_H
3 #define FRAMELIB_GLOBAL_H
4 
5 #include "FrameLib_Types.h"
6 #include "FrameLib_Errors.h"
7 #include "FrameLib_Memory.h"
9 #include "FrameLib_Threading.h"
10 
11 #include <memory>
12 #include <vector>
13 
29 {
30 
31  friend class FrameLib_Context;
32 
33 private:
34 
35  template <class T> class PointerSet
36  {
37  // A simple countable pointer with a reference address
38 
39  struct CountablePointer
40  {
41  CountablePointer(T* object, void *reference) : mObject(object), mReference(reference), mCount(1) {}
42 
43  std::unique_ptr<T> mObject;
44  void *mReference;
45  long mCount;
46  };
47 
48  public:
49 
50  // Add, find or release an object by reference address
51 
52  void add(T *object, void *reference);
53  T *find(void *reference);
54  void release(void *reference);
55 
56  private:
57 
58  // Internal Pointers
59 
60  std::vector<CountablePointer> mPointers;
61  };
62 
63 public:
64 
76  static FrameLib_Global *get(FrameLib_Global **global, FrameLib_ErrorReporter::HostNotifier *notifier = nullptr);
77 
87  static void release(FrameLib_Global **global);
88 
89 private:
90 
91  // Constructor / Destructor
92 
93  FrameLib_Global(FrameLib_ErrorReporter::HostNotifier *notifier) : FrameLib_ErrorReporter(notifier), mAllocator(*this), mCount(0) {}
94  ~FrameLib_Global() {};
95 
96  // Non-copyable
97 
98  FrameLib_Global(const FrameLib_Global&) = delete;
99  FrameLib_Global& operator=(const FrameLib_Global&) = delete;
100 
101  // Methods to retrieve common objects
102 
103  FrameLib_LocalAllocator *getAllocator(void *reference);
104  FrameLib_ProcessingQueue *getProcessingQueue(void *reference);
105 
106  // Methods to release common objects
107 
108  void releaseAllocator(void *reference);
109  void releaseProcessingQueue(void *reference);
110 
111  // Reference Counting / Auto-deletion
112 
113  void increment();
114  FrameLib_Global *decrement();
115 
116  // Member Variables
117 
118  // Global Memory Allocator
119 
120  FrameLib_GlobalAllocator mAllocator;
121 
122  // Context-specific Resources
123 
124  PointerSet<FrameLib_LocalAllocator> mLocalAllocators;
125  PointerSet<FrameLib_ProcessingQueue> mProcessingQueues;
126 
127  // Lock and Reference Count
128 
129  FrameLib_SpinLock mLock;
130  long mCount;
131 };
132 
133 #endif
a class for containing and managing FrameLib&#39;s global resources.
Definition: FrameLib_Global.h:28
a class used to represent distinct non-connectable areas in the host environment. ...
Definition: FrameLib_Context.h:21
static void release(FrameLib_Global **global)
Definition: FrameLib_Global.cpp:61
a spinlock that can be locked, attempted or acquired.
Definition: FrameLib_Threading.h:84
a class used to report errors to the host environment.
Definition: FrameLib_Errors.h:34
a minimal processing queue that is used to non-recursively process FrameLIB_DSP objects in a network...
Definition: FrameLib_ProcessingQueue.h:30
a global threadsafe memory allocator suitable for realtime usage.
Definition: FrameLib_Memory.h:31
a memory allocator suitable for usage in a given FrameLib context.
Definition: FrameLib_Memory.h:245
a virtual struct used to supply a method for notifying the host of errors.
Definition: FrameLib_Errors.h:47